package com.solr.sboot.solr.service.impl;

import com.solr.sboot.solr.po.DocumentPo;
import com.solr.sboot.solr.repository.DocumentRepository;
import com.solr.sboot.solr.service.DocumentService;
import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.util.NamedList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.List;

/**
 * @描述: 查询实例
 * @类名:
 * @作者: yangshuang
 * @创建时间: 2017/8/21 14:16
 * @版本: v1.0
 */
@Service
@Transactional
public class DocumentServiceImpl implements DocumentService {


    @Autowired
    private SolrClient solrClient;

    @Autowired
    private DocumentRepository documentRepository;

    @Override
    public List<DocumentPo> findById(String id) {
        return documentRepository.findByQueryAnnotation(id);
    }

    @Override
    public Long getDocumentCount() {
        return documentRepository.count();
    }

    @Override
    public Page<DocumentPo> findByName(String searchTerm, Pageable pageable) {
        if (StringUtils.isBlank(searchTerm)) {
            documentRepository.findAll(pageable);
        }
        SolrInputDocument solrInputDocument = new SolrInputDocument();
        try {
            NamedList<Object> response = solrClient.add(solrInputDocument).getResponse();
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void addDocument(DocumentPo documentPo) {
        documentRepository.save(documentPo);//不存在的时候记录数据,存在的时候更新数据
    }

    @Override
    public List<DocumentPo> findByCustomQuery(String searchName, Pageable pageable) {
        Page<DocumentPo> byCustomQuery = documentRepository.findByCustomQuery(searchName, pageable);
        return null;
    }

}
